/*
AgencyHRPopulate
Version 1.0
Description: Class contains methods which populate bind field in Agencgy_Team__c with a HR__c record.
Author: Pratibh Prakash
Created Date: 13 Feb-2009
*/
public class AgencyHRPopulate{
    /*
    populateHR()
    @param objAgency: List for storing Agency_Team__c[] records.
    Description: Method relates HR field of Agency_Team__c with HR__c record if N# of HR__c
    matches with N# retrieved by parsing Legacy Id field of Agency_Team__c.
    */
    public static void populateHR(Agency_Team__c[] objAgency){
        
        Map<String,Agency_Team__c> N_List = new  Map<String,Agency_Team__c>();//Map for storing Unique N# value and its Agency_Team_c record as key value pair.
        Integer indexLast_N;//Stores last index of N in Legacy Id field of Agency_Team__c record.
        
        for(Agency_Team__c agency: objAgency){
            
            if(agency.Legacy_ID__c != null){
                if((agency.Legacy_ID__c).length()>0){
                
                    indexLast_N = (agency.Legacy_ID__c).lastIndexOf('N');
                    if(indexLast_N>-1){
                        N_List.put((agency.Legacy_ID__c).substring(indexLast_N, (agency.Legacy_ID__c).length()), agency);
                    }
                }
            }
       }

        HR__c[] objHR = [select Id, N__c, User__c from HR__c where N__c in : N_List.keyset()];//Getting HR__c records which have N# in Legacy ID field in Agency_Team__c record.
        
        if(objHR.size()>0){
            
            for(HR__c hr:objHR){
                    (N_List.get(hr.N__c)).HR_Name__c = hr.Id;
                    (N_List.get(hr.N__c)).User__c = hr.User__c;
            }
        }
        
    }
}